package com.hospital.dictionary.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.common.core.exception.ExceptionHelp;
import com.hospital.common.core.exception.ExceptionHelpEnum;
import com.hospital.dictionary.constant.CommonConstant;
import com.hospital.dictionary.module.entity.ClinicItemGuide;
import com.hospital.dictionary.module.entity.ClinicItemGuideKey;
import com.hospital.dictionary.module.entity.ClinicItemGuideValue;
import com.hospital.dictionary.module.mapper.ClinicItemGuideKeyMapper;
import com.hospital.dictionary.module.mapper.ClinicItemGuideMapper;
import com.hospital.dictionary.module.mapper.ClinicItemGuideValueMapper;
import com.hospital.dictionary.module.service.ClinicItemGuideKeyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * ÕïÁÆÏîÄ¿Ö¸ÒýÐÅÏ¢Ä£°åÅäÖÃ 服务实现类
 * </p>
 *
 * @author lvyao
 * @since 2024-01-10
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ClinicItemGuideKeyServiceImpl extends ServiceImpl<ClinicItemGuideKeyMapper, ClinicItemGuideKey> implements ClinicItemGuideKeyService {
    @Resource
    private ClinicItemGuideKeyMapper clinicItemGuideKeyMapper;

    @Resource
    private ClinicItemGuideValueMapper clinicItemGuideValueMapper;

    @Resource
    private ClinicItemGuideMapper clinicItemGuideMapper;

    @Override
    public boolean updateAll(ClinicItemGuideKey object, int sortNoOld) {
        //获取当前修改后的sortno是否已存在
        QueryWrapper<ClinicItemGuideKey> queryWrapper = new QueryWrapper<ClinicItemGuideKey>().eq(CommonConstant.GUIDE_CODE,object.getGuideCode())
                .eq(CommonConstant.SORT_NO,object.getSortNo());
        List<ClinicItemGuideKey> list = clinicItemGuideKeyMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(list)){
            list.stream().forEach(clinicItemGuideKey -> {
                Integer sortNo = clinicItemGuideKey.getSortNo();
                clinicItemGuideKey.setSortNo(sortNoOld);
               if (!updateNewAndOldSort(clinicItemGuideKey,sortNo)){
                   throw new ExceptionHelp(ExceptionHelpEnum.NOTICE_UPDATE_SORT_VALUE_OLD_ERR);
               }
            });
        }
        if (!updateNewAndOldSort(object,sortNoOld)){
            throw new ExceptionHelp(ExceptionHelpEnum.NOTICE_UPDATE_SORT_VALUE_OLD_ERR);
        }
        return true;
    }
    private boolean updateNewAndOldSort(ClinicItemGuideKey object, int sortNoOld){
        List<ClinicItemGuide> clinicItemGuides = clinicItemGuideMapper.selectList(new QueryWrapper<ClinicItemGuide>().eq(CommonConstant.GUIDE_KEY_CODE, object.getGuideCode()));
        List<String> guideValueList = clinicItemGuides.stream().map(ClinicItemGuide::getGuideValueCode).distinct().collect(Collectors.toList());
        List<ClinicItemGuideValue> clinicItemGuideValues = clinicItemGuideValueMapper.selectList(new QueryWrapper<ClinicItemGuideValue>().in(CommonConstant.GUIDE_CODE, guideValueList).eq(CommonConstant.SORT_NO, sortNoOld));
        try {

            int update = clinicItemGuideKeyMapper.updateById(object);
            if (update>0){
                clinicItemGuideValues.stream().forEach(clinicItemGuideValue -> {
                    clinicItemGuideValue.setSortNo(object.getSortNo());
                    int updateValue = clinicItemGuideValueMapper.updateById(clinicItemGuideValue);
                    if (updateValue==0){
                        throw new ExceptionHelp(ExceptionHelpEnum.NOTICE_UPDATE_SORT_VALUE_ERR);
                    }
                });
            }
        } catch (Exception e) {
            ExceptionHelpEnum.NOTICE_UPDATE_SORT_ERR.setMessage(ExceptionHelpEnum.NOTICE_UPDATE_SORT_ERR + e.getMessage());
            throw new ExceptionHelp(ExceptionHelpEnum.NOTICE_UPDATE_SORT_ERR);
        }
        return true;
    }
}
